<?php

namespace app\index\controller;

use think\Controller;
use think\Session;

class Logup extends Controller
{
    public function _initialize()
    {
        if(Session::has('Username')&&Session::has('UserId'))
        {
            $this->success('您已登录!','../');
        }
    }
    
    public function index()
    {
    	return $this->fetch('logup');
    }

    public function createuser()
    {
    	$user = model('User');
    	$param = request()->param();
    	$data = $user->createuser($param);
    	if($data==='phonealr'){
    		return json(['message'=>'电话号码已重复']);
    	}else if($data==='emailalr'){
    		return json(['message'=>'邮箱已重复']);
    	}else if($data){
    		if(!$this->sendemail($param)){
    			return json(['message'=>'創建成功!']);
    		}
    		return json(['message'=>'邮件发送失败!']);
    	}else{
    		return json(['message'=>$user->getError()]);
    	}
    }

    protected function sendemail($param)
    {
    	$toemail=$param['email'];
        $name='伯乐王';
        $subject='账号激活邮件';
        $param['password'] = pwd_md5($param['password']);
        $url = 'http://bolewang.com:8080/index/oauth/id/'.$param['password'];
        $content='感谢您注册伯乐王网站,请点击以下链接完成激活'.$url;
        send_mail($toemail,$name,$subject,$content);
    }
}
